package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.menum.AdPosition;
import com.cl.short_video_java.pojo.VideoAd;
import com.cl.short_video_java.service.SysSettingService;
import com.cl.short_video_java.service.VideoAdService;
import com.cl.short_video_java.mapper.VideoAdMapper;
import com.cl.short_video_java.vo.video.Advertis;
import com.cl.short_video_java.vo.video.AdvertisSearch;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author object
 * @description 针对表【video_ad】的数据库操作Service实现
 * @createDate 2024-10-31 15:58:59
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class VideoAdServiceImpl extends ServiceImpl<VideoAdMapper, VideoAd>
		implements VideoAdService {
	private final SysSettingService settingService;

	@Override
	public PageInfo<VideoAd> getPageService(AdvertisSearch advertisSearch) {
		if (advertisSearch.getPageSize() == null || advertisSearch.getPageNum() == null) {
			advertisSearch.setPageNum(1);
			advertisSearch.setPageSize(20);
		}
		PageHelper.startPage(advertisSearch.getPageNum(), advertisSearch.getPageSize());
		List<VideoAd> list = this.list(new QueryWrapper<VideoAd>()
				.like(!advertisSearch.getTitle().equals(""), "title", advertisSearch.getTitle())
				.eq(advertisSearch.getId() != null, "id", advertisSearch.getId())
				.between((advertisSearch.getBeginTime() != null && advertisSearch.getEndTime() != null), "create_time", advertisSearch.getBeginTime(), advertisSearch.getEndTime()));
		return new PageInfo<VideoAd>(list);
	}

	@Override
	public Object deleteAdvertisService(String idlist) {
		String[] split = idlist.split(",");
		boolean removed = this.removeBatchByIds(Arrays.stream(split).toList());
		return removed;
	}

	@Override
	public Object updataPosition(Integer position, String idlist) {
		String[] split = idlist.split(",");
		String positionname = AdPosition.getValueByKey(position);

		if (split.length > 0 && !positionname.equals("")) {
			boolean update = this.update(new UpdateWrapper<VideoAd>()
					.in("id", Arrays.stream(split).toList())
					.set("position", positionname));
			return update;
		} else {
			throw new RuntimeException("参数为空");
		}

	}

	@Override
	public Boolean setStatusService(Integer id, Integer status) {
		boolean update = this.update(new UpdateWrapper<VideoAd>()
				.eq("id", id)
				.set("status", status));
		return update;
	}

	/**
	 * 广告添加
	 *
	 * @param advertis 添加参数
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(Advertis advertis) {
		VideoAd videoAd = new VideoAd();
		videoAd.setTitle(advertis.getTitle());
		videoAd.setCreateTime(new Date());
		videoAd.setPosition(AdPosition.getValueByKey(advertis.getPosition()));
		videoAd.setImgUrl(advertis.getImgUrl());
		videoAd.setTargetUrl(advertis.getTargetUrl());
		videoAd.setStatus(advertis.getStatus());
		this.save(videoAd);
	}

	/**
	 * 获取分类页的广告
	 *
	 * @return
	 */
	@Override
	public Map<String, List<VideoAd>> typeAd() {
		Integer top = Integer.valueOf(settingService.getSysSettingByKey("sys_ad_detail_top"));
		Integer bottom = Integer.valueOf(settingService.getSysSettingByKey("sys_ad_detail_bottom"));
		Map<String, List<VideoAd>> map = new HashMap<>();
		if (top == 1) {
			List<VideoAd> list = this.list(new QueryWrapper<VideoAd>()
					.eq("position", AdPosition.TYPE_TOP.getPosition())
					.eq("status", 0));
			map.put("top", list);
		}

		if (bottom == 1) {
			List<VideoAd> list = this.list(new QueryWrapper<VideoAd>()
					.eq("position", AdPosition.TYPE_BOTTOM.getPosition())
					.eq("status", 0));
			map.put("bottom", list);
		}
		return map;
	}

	/**
	 * 首页的广告
	 *
	 * @return
	 */
	@Override
	public Map<String, List<VideoAd>> homeAd() {
		Integer top = Integer.valueOf(settingService.getSysSettingByKey("sys_ad_home_top"));
		Integer bottom = Integer.valueOf(settingService.getSysSettingByKey("sys_ad_home_bottom"));
		Map<String, List<VideoAd>> map = new HashMap<>();
		if (top == 1) {
			List<VideoAd> list = this.list(new QueryWrapper<VideoAd>()
					.eq("position", AdPosition.HOME_TOP.getPosition())
					.eq("status", 0));
			map.put("top", list);
		}

		if (bottom == 1) {
			List<VideoAd> list = this.list(new QueryWrapper<VideoAd>()
					.eq("position", AdPosition.HOME_BOTTOM.getPosition())
					.eq("status", 0));
			map.put("bottom", list);
		}
		return map;
	}
}




